package guowei.guliorder.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import guowei.guliorder.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * 用于测试支付宝支付的工具类
 */
@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "9021000131661994";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCGtf28feHt4iqHzY4RUSMzRt+iuzntmK2z4H8SzwL7MOg2VdIXV50fTVE1mxIk6M+tT6tJ/LJ0mWawOozFQrCv5rXabdwKK13nBPvRPsG1jm448v7not/OxW6ySXmJLZxpFfCG0Z5b2MXYC7ZmUFg5ypmfTUEkL7UkPr6v57DDlm3tSm13HVPgqg4NsgOyQeTmKUM9zGYilsNjD5QGw0XGHC6baMh2r0UdH08hCM7POXVgPO/gloRhFByRm/xebd7q+MZIN18BCKAmyVatDPdDCi/vGddZ7G7myKdpNE/etiqpcaE5VTYfb4FgUGVjbpbQxtNbX2BJUGXQI1OgqGUfAgMBAAECggEAbNLyguYysOinMQQGg8jHJfJYUW7skhtoH8r4/OnM372XqeVUjlTR8mqUczitxLFqq/iTSHbidz1BbpYuCGEiuUcom6dqPbDg7/Lftx76f7/rHQh4kgnKwZBPJ6ohi4cUNvRpVZBbXJlW4nw7wpWfYDhiOAXB3QjJTdl+VD5YVvehgPPC7juAgq4X3IV8rVh0/eUG6XHGaEBUDkzTf04Jd/fgJjaHjpKDBUtoDU/Mja3BPce9ASfN7c14VqEihzNYwdGIB4AjGBJDMX25fH1PFuSXkWCojI30DE4RoFOO3+56J9vxdnW5tgGV1R0IiVpymhbeKM59dslpNgEcz7vK2QKBgQD9UvLJW4TVYPf2QFhrkOMEExFyX02ioit8Nf0HUyfi2ua2JFyPU1byklgmM4YG6orsaiNuWG9ESKeX5NwJEOfvfm5vCASETzo3J69m1vGhs2h95dfe4GSvFkF4lhtIFi2I87KUKSrMON4RFM39myqD+WN42PfvZEX0WOMcxrxDFQKBgQCIIkh/RtOpRFaeisjJtB1GXbWDbFo85Nny7REl+R4xxtXdXB2+MCnNmU5mMYppZxODSDCsYjJKBjxvOXtEjNzQGXtFB+d8rdgpboEV4o8TfUSy80AxCIKipq1dDIvP2F4vq6ZHHCvJPccbf4nwzQOr2FBw7sMR/7Qn3Uamq52kYwKBgHMHemWCoDPAP3hbSYs2lE8iVXbI3djuqK5TWZ083ky9t6uXoUwXvNCTMWHPXZnAYOrvWz1c/NFV6HwpmNypB5iRoZXbXymxTC8FXzlkev5BGD6q4rY60J1Mjtgx2RuHro+UkU+DzgJEBFpfRxZUoh6hXNDgPN1AfB9zSVWkzJHtAoGAAZMT75TsIcsOjWrhg6O3qvaugMSiDo4/DxY62ouVE9JO6u6D0Y1RDnW1eG0eQrXUffI5zeSc3EpSLJ8nk9VlQGrrCBNdVA3WsDP8/jnOmdUbFE7H8Uk96Y+v9aGNhGvmOvrrQ2tJ/lvej6zb9ZMJddVcqNG6I90nQkbG6GqXui0CgYEArd7fQeMbiz5cxI6Tm6yCzjRS621QoHPLyqovO4DV3X7/WbmbLQiGeiiF3jiAnCY2Ky2kDnI5VFBgEOkq9VVSC3Qh4Sds2yi503XEnqzcoQmnMAfYBZWa4Hls7IV0a1WNuTTwBDR8ma8yc14bRHKTehGK3jr9wEVbf+YGXu8Z040=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAn/jZal8npKqXi0kic8YtdFi3stzsmhn4OBxcBjp83OYspVc1px1FeheduKPHINjpO7b0iXGhgbgkVHQuHlLHPebCWjGtt/LfmRkPkVtFXukm+C9Z1sZj1m+QRlFPGpm9ifzzEupyBhznFZYElduW84yx2nC/GHFRP1MlzO2m5E3F7DJaoMbXeeTlnIgp9Cv1N4kALVjR2S0VNBD/jB1L0Tqn4AU3UHrr0WGypzdhiKt49OxiqvkmKr6pBbhZFfoDfRAWtzXSca7yRRdMMZahsccc3vnD5ZOkIvVc0OWv+YkbfCejybn0BfcoSj7SVTRclAaVOiHfOSYWlyma6022BwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url = "https://77023869.r1.cpolar.top/order/alipay";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.ganjiuwanle.com/orderlist.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    private String timeout = "1m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+ timeout +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
